package com.springboot.framework.controller;

import com.springboot.framework.annotation.ACS;
import com.springboot.framework.controller.request.CategoryInsertBean;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.CategoryModel;
import com.springboot.framework.service.CategoryService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author SWF
 * @date 2019/6/25 14:40
 **/
@Api(description = "【管理员】商品品类")
@RestController
@RequestMapping("/admin/category")
public class AdminProductCategoryController {
    @Resource
    private CategoryService categoryService;

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "新增品类",notes = "新增品类")
    @PostMapping("/insert")
    public ResponseVO insert(@RequestBody @Valid CategoryInsertBean bean , BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        CategoryModel categoryModel = categoryService.insert(convertCategoryModel(bean));
        return ResponseVoUtil.success(categoryModel);
    }

    private CategoryModel convertCategoryModel(CategoryInsertBean bean){
        CategoryModel categoryModel = new CategoryModel();
        BeanUtils.copyProperties(bean,categoryModel);
        return categoryModel;
    }
}
